package com.mikhailenko.transportMVC.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.mikhailenko.transportMVC.exeption.UserNotFoundExeption;
import com.mikhailenko.transportMVC.model.User;
import com.mikhailenko.transportMVC.service.UserService;
import com.mikhailenko.transportMVC.utils.MD5Util;
import com.mikhailenko.transportMVC.validator.UserValidator;

@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private UserValidator userValidator;
	
	@InitBinder
	private void initBinder(WebDataBinder binder) {
		binder.setValidator(userValidator);
	}

//	private static final Logger log = Logger.getLogger(UserController.class
//			.toString());

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView newUserPage() {
		ModelAndView mav = new ModelAndView("user-new", "user", new User());
		return mav;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public ModelAndView createNewUser(@ModelAttribute @Valid User user,
			BindingResult result,
			final RedirectAttributes redirectAttributes) {

		if (result.hasErrors()){
			return new ModelAndView("user-new");
		}
		
		ModelAndView mav = new ModelAndView();
		String message = "New user " + user.getUsername()
				+ " was successfully created.";
		String password = MD5Util.encode(user.getPassword());
		user.setPassword(password);
		userService.create(user);
		mav.setViewName("redirect:/");

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView userListPage() {
		ModelAndView mav = new ModelAndView("user-list");
		List<User> userList = userService.findAll();
		mav.addObject("userList", userList);
		return mav;
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editUserPage(@PathVariable Integer id) {
		ModelAndView mav = new ModelAndView("user-edit");
		User user = userService.findById(id);
		mav.addObject("user", user);
		return mav;
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public ModelAndView editUser(@ModelAttribute @Valid User user,
			BindingResult result,
			@PathVariable Integer id,
			final RedirectAttributes redirectAttributes)
			throws UserNotFoundExeption {

		if (result.hasErrors()){
			ModelAndView mav = new ModelAndView("user-edit");
			user.setUserID(id);
			mav.addObject("user", user);
			return mav;
		}
		
		user.setUserID(id);
		user.setPassword(MD5Util.encode(user.getPassword()));
		userService.update(user);

		ModelAndView mav = new ModelAndView("redirect:/");
		String message = "User:"+user.getUsername()+" was successfully updated.";

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteUser(@PathVariable Integer id,
			final RedirectAttributes redirectAttributes)
			throws UserNotFoundExeption {

		ModelAndView mav = new ModelAndView("redirect:/index.jpa");

		User user = userService.delete(id);
		String message = "The user " + user.getUsername()
				+ " was successfully deleted.";

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}

}
